package sjtu.ist.warehouse4ws.dao.cust;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.springframework.orm.hibernate3.HibernateTemplate;

import sjtu.ist.warehouse4ws.dao.BaseDaoHibernate;
import sjtu.ist.warehouse4ws.model.CustFileinfo;
import sjtu.ist.warehouse4ws.model.Fileinfo;

public class CustFileInfoDaoHibernate extends BaseDaoHibernate<CustFileinfo> implements CustFileInfoDao {

	public CustFileinfo get(String id) {
		return (CustFileinfo)getHibernateTemplate().get(CustFileinfo.class, id);
	}

	public String save(CustFileinfo CustFileinfo) {
		getHibernateTemplate().setFlushMode(HibernateTemplate.FLUSH_EAGER); 
		return (String)getHibernateTemplate().save(CustFileinfo);
	}

	public void update(CustFileinfo CustFileinfo) {
		
		getHibernateTemplate().update(CustFileinfo);
	}

    public void delete(String id) {
		CustFileinfo fi=get(id);
		getHibernateTemplate().delete(fi);
		
		int type=fi.getType();
		String delfilehql = "delete Privilege where CustFileinfoId=?";
		Query query2 = getSession().createQuery(delfilehql);
		query2.setString(0, fi.getId());
		int n = query2.executeUpdate();
		
		if(type==1){
			String hql = "delete Process where fileId=?";
			Query query = getSession().createQuery(hql);
			query.setString(0, fi.getId());
			int x = query.executeUpdate();
		}else if(type==2){
			String hql = "delete Functionpoint where fileId=?";
			Query query = getSession().createQuery(hql);
			query.setString(0, fi.getId());
			int x = query.executeUpdate();
		}else if(type==3){
			
		}else if(type==4){
			String hql = "delete Domainmodel where fileId=?";
			Query query = getSession().createQuery(hql);
			query.setString(0, fi.getId());
			int x = query.executeUpdate();
		}else if(type==5){
			String hql = "delete Epcunits where fileId=?";
			Query query = getSession().createQuery(hql);
			query.setString(0, fi.getId());
			int x = query.executeUpdate();
		}else if(type==6){
//			String hql = "delete Epcunits where fileId=?";
//			Query query = getSession().createQuery(hql);
//			query.setString(0, fi.getId());
//			int x = query.executeUpdate();
		}else if(type==7){
			String hql = "delete Role where fileId=?";
			Query query = getSession().createQuery(hql);
			query.setString(0, fi.getId());
			int x = query.executeUpdate();
		}
	}


	public void delete(CustFileinfo CustFileinfo) {
		
		getHibernateTemplate().delete(CustFileinfo);
	}

	public List<CustFileinfo> findByName(String name) {

		return (List<CustFileinfo>)getHibernateTemplate().find("from CustFileinfo u where u.name='"+name+"'");
	}
	
	//模型普通搜索
	public List<CustFileinfo> findByKeyword(String keyWord) {
		//暂时先查文件名，有待修改
		String sql = "where (u.name like '%"+keyWord+"%'"
		+ "or u.version like '%"+keyWord+"%' "
		+ "or d.cnName like '%"+keyWord+"%' "
		+ "or u.project like '%"+keyWord+"%' "
		+ "or c.name like '%"+keyWord+"%') "
		+ "and u.company = c.id and u.department = d.id and u.type !=0";
		System.out.println(sql);
		return (List<CustFileinfo>)getHibernateTemplate().find(
				"select u from CustFileinfo u, Company c, CustDepartment d "+sql);
	}
	
	//模型高级搜索
	public List<CustFileinfo> findDetailByKeyword(String keyWord1, String keyWord2, String keyWord3,
												String keyWord4, String keyWord5, String keyWord6) {
		//暂时先查文件名，有待修改
		System.out.println("CustFileinfoDaoHibernate-Detail");
		String sql=" ";
		if(keyWord1 != null && !keyWord1.equals("")){
			sql = sql + "and u.name like '%"+keyWord1+"%'";
		}
		if(keyWord2 != null && !keyWord2.equals("")){
			sql = sql + "and u.project like '%"+keyWord2+"%'";
		}		
		if(keyWord3 != null && !keyWord3.equals("")){
			sql = sql + "and u.department = '"+keyWord3+"'";
		}
		if(keyWord4 != null && !keyWord4.equals("")){
			sql = sql + "and u.version like '%"+keyWord4+"%'";
		}
		if(keyWord5 != null && !keyWord5.equals("")){
			sql = sql + "and c.name like '%"+keyWord5+"%'";
		}
		if(keyWord6 != null && !keyWord6.equals("")){
			sql = sql + "and u.type = '"+keyWord6+"'";
		}

		System.out.println(sql);
		return (List<CustFileinfo>)getHibernateTemplate().find("select u from CustFileinfo u, Company c where u.company = c.id "+sql);
	}

	public List<CustFileinfo> getAllCustFileInfos(){
		return (List<CustFileinfo>)getHibernateTemplate().find("from CustFileinfo");
	}
	
	@Override
	public List<CustFileinfo> getFileId(String fileName, String project, int type) {
		// TODO Auto-generated method stub
		SQLQuery query=getSession().createSQLQuery("select * from cust_fileinfo where name='"+fileName+"' and project='"+project+"' and type='"+3+"'");
		List list=query.addEntity(CustFileinfo.class).list();
		return (List<CustFileinfo>) list;
	}
}
